﻿using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.IFModels;
using Estsh.Core.Models;
using Estsh.Core.Quartz.BaseService;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;

namespace Estsh.Core.Wms.Jobs
{
    /// <summary>
    /// 大件物料拉动根据JIT信息自动产生 扫描拣选单JOB
    /// </summary>
    public class JitPullJob : IJobService
    {
        private readonly IPullingRepository pullRepository;
        private readonly IMoveOrderRepository moveOrderRepository;



        public JitPullJob(IPullingRepository _pullGroupRepository,
            IMoveOrderRepository _moveOrderRepository)
        {
            this.pullRepository = _pullGroupRepository;
            this.moveOrderRepository = _moveOrderRepository;

        }

        public string ExecuteService(string parameter)
        {
            try
            {

				#region 原来的逻辑

                /*
				List<MesWmsWorkOrder> mesWmsWorks = pullRepository.GetProductionQueueInfo();

				if (mesWmsWorks.Count > 0)
				{
					pullRepository.InsertProductionQueueInfo(mesWmsWorks); //"待处理的JIT生产队列！";
				}

				List<WmsPullingGroup> pullingGroups = pullRepository.GetPullingGroupList((int)WmsEnumUtil.PullGroupType.PRODUCT_JIT);
				if (pullingGroups.Count <= 0)
				{
					return "没有维护JIT拉动组！";
				}
				List<MesWorkOrder> workOrders = pullRepository.GetPullingJitOrderList();
				if (workOrders.Count <= 0)
				{
					return "没有待拉动的JIT数据！";
				}

				foreach (var pullingGroup in pullingGroups)
				{
					List<WmsPullingOrderDetail> moveDetails = pullRepository.GetPullingPickPartList(pullingGroup.GroupCode, (int)MesEnumUtil.WorkOrderType.PRODUCT_JIT);
					if (moveDetails.Count <= 0)
					{
						continue;
					}

					moveOrderRepository.CreatePullPickOrder("0", moveDetails, pullingGroup);
				}
				return "生成拉动单成功！";

                */
				#endregion

				List<MesWmsWorkOrder_Extend> mesWmsWorks = pullRepository.GetProductionQueueExtendInfo();

				if (mesWmsWorks.Count > 0)
                {
                    pullRepository.InsertProductionQueueExtendInfo(mesWmsWorks); //"待处理的JIT生产队列！";
                }



                List<WmsPullingGroup> pullingGroups = pullRepository.GetPullingGroupList((int)WmsEnumUtil.PullGroupType.PRODUCT_JIT);
                if (pullingGroups.Count <= 0)
                {
                    return "没有维护JIT拉动组！";
                }
                List<MesWorkOrder> workOrders = pullRepository.GetPullingJitOrderList();
                if (workOrders.Count <= 0)
                {
                    return "没有待拉动的JIT数据！";
                }

                foreach (var pullingGroup in pullingGroups)
                {
                    List<WmsPullingOrderDetail> moveDetails = pullRepository.GetPullingPickPartList(pullingGroup.GroupCode, (int)MesEnumUtil.WorkOrderType.PRODUCT_JIT);
                    if (moveDetails.Count <= 0)
                    {
                        continue;
                    }

                    moveOrderRepository.CreatePullPickOrder("0", moveDetails,pullingGroup);
                }
                return "生成拉动单成功！";




            }
            catch (Exception ex) 
            {
                LogHelper.Error("生成拉动单错误！" + ex);
                return "生成拉动单错误！" + ex.Message;
            }
            
        }
    }
}
